package org.xxpay.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xxpay.core.entity.PayInterfaceExample;
import org.xxpay.core.entity.PayInterfaceType;
import org.xxpay.core.entity.PayInterfaceTypeExample;
import org.xxpay.core.service.IPayInterfaceTypeService;
import org.xxpay.service.dao.mapper.PayInterfaceMapper;
import org.xxpay.service.dao.mapper.PayInterfaceTypeMapper;
import org.xxpay.service.dao.mapper.PayPassageMapper;

import java.util.List;

/**
 * @author: dingzhiwei
 * @date: 2018/5/3
 * @description: 支付接口类型
 */
@Service
public class PayInterfaceTypeServiceImpl implements IPayInterfaceTypeService {

    @Autowired
    private PayInterfaceTypeMapper payInterfaceTypeMapper;

    @Autowired private PayInterfaceMapper payInterfaceMapper;

    @Autowired
    private PayPassageMapper payPassageMapper;

    @Override
    public int add(PayInterfaceType payInterfaceType) {
        return payInterfaceTypeMapper.insertSelective(payInterfaceType);
    }

    @Override
    public int update(PayInterfaceType payInterfaceType) {
        return payInterfaceTypeMapper.updateByPrimaryKeySelective(payInterfaceType);
    }

    @Override
    public PayInterfaceType findByCode(String ifTypeCode) {
        return payInterfaceTypeMapper.selectByPrimaryKey(ifTypeCode);
    }

    @Override
    public List<PayInterfaceType> select(int offset, int limit, PayInterfaceType payInterfaceType) {
        PayInterfaceTypeExample example = new PayInterfaceTypeExample();
        example.setOrderByClause("createTime DESC");
        example.setOffset(offset);
        example.setLimit(limit);
        PayInterfaceTypeExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, payInterfaceType);
        return payInterfaceTypeMapper.selectByExample(example);
    }

    @Override
    public Integer count(PayInterfaceType payInterfaceType) {
        PayInterfaceTypeExample example = new PayInterfaceTypeExample();
        PayInterfaceTypeExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, payInterfaceType);
        return payInterfaceTypeMapper.countByExample(example);
    }

    @Override
    public List<PayInterfaceType> selectAll(PayInterfaceType payInterfaceType) {
        PayInterfaceTypeExample example = new PayInterfaceTypeExample();
        example.setOrderByClause("createTime DESC");
        PayInterfaceTypeExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, payInterfaceType);
        return payInterfaceTypeMapper.selectByExample(example);
    }

    @Override
    @Transactional(transactionManager = "transactionManager", rollbackFor = Exception.class)
    public void deletePayInterface(String ifTypeCode) {

        payInterfaceTypeMapper.deleteByPrimaryKey(ifTypeCode);

        PayInterfaceExample exa = new PayInterfaceExample();
        exa.createCriteria().andIfTypeCodeEqualTo(ifTypeCode);
        payInterfaceMapper.deleteByExample(exa);
    }

    void setCriteria(PayInterfaceTypeExample.Criteria criteria, PayInterfaceType obj) {
        if(obj != null) {
            if(StringUtils.isNotEmpty(obj.getIfTypeCode())) criteria.andIfTypeCodeEqualTo(obj.getIfTypeCode());
            if(StringUtils.isNotEmpty(obj.getIfTypeName())) criteria.andIfTypeNameLike("%" + obj.getIfTypeName() + "%");
            if(obj.getStatus() != null && obj.getStatus().byteValue() != -99) criteria.andStatusEqualTo(obj.getStatus());
            if(obj.getMustFromTemplate() != null) criteria.andMustFromTemplateEqualTo(obj.getMustFromTemplate());

            //查询可见ifTypeCode
            List<String> ifTypeCodeInList = obj.getPsVal("ifTypeCodeInList", List.class);
            if(ifTypeCodeInList != null){
                if(ifTypeCodeInList.isEmpty()){ //in = null 避免报错， 添加 1 != 1
                    criteria.andIfTypeCodeIsNull();
                }else{
                    criteria.andIfTypeCodeIn(ifTypeCodeInList);
                }

            }
        }
    }
}
